package fr.pascal.web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

import fr.pascal.entities.Abonnement;
import fr.pascal.entities.AbonnementGsm;
import fr.pascal.entities.AbonnementInternet;
import fr.pascal.service.IAbonnementService;

@Component
public class AbonnementAction extends ActionSupport {

	private static final long serialVersionUID = 1049551991106886718L;
	
	@Autowired
	private IAbonnementService service;
	
	public Long idAbonnement;
	public Date dateAbonnement;
	public double solde;
	public boolean actif;
	public String type; 
	public int fidelio;
	private int debit;
	public String[] typesAbos = new String[]{"GSM","INTERNET"};
	public List<Abonnement> abonnements;

	public String index() {
		abonnements = service.listeAbonnements(true);
		return SUCCESS;
	}
	
	public String getSubForm() {
		return SUCCESS;
	}
	
	public String save() {
		Abonnement ab;
		if ("GSM".equals(type)) {
			ab = new AbonnementGsm(dateAbonnement, solde, actif, fidelio);
		} else {
			ab = new AbonnementInternet(dateAbonnement, solde, actif, debit);
		}
		service.addAbonnement(ab);
		abonnements = service.listeAbonnements(true);
		return SUCCESS;
	}
}
